﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class GazeController : MonoBehaviour {
	//the canvas that hold your reticle;
	public Canvas canvasPoint;
	public Image reticleImage;
	//gaze default distance
	public float deafaultGazeDistance;

	//store interactibated Objs
	private GameObject curInteractiveObj;
	private GameObject oldInteractiveObj;
	private Peter_InteractiveObj objDoing;
	private Peter_InteractiveObj oldobjDoing;

	// canvas position and rotation. will back to original,when no item dected;
	private Vector3 canvasOriginalPosition;
	private Vector3 canvasOriginalRotation;
	private Vector3 canvasOriginalScale;

	// Use this for initialization
	void Start () {
		// record the original paramters for canvas/reticle
		canvasOriginalPosition =new Vector3(0,0,deafaultGazeDistance);
		canvasOriginalRotation = canvasPoint.transform.eulerAngles;
		canvasOriginalScale = new Vector3 (0.02f, 0.02f, 0.02f);
	}
	
	// Update is called once per frame
	void Update () {
		//a ray cast from camera,and toward to cameras direction;
		Ray ray = new Ray (transform.position, transform.forward);
		RaycastHit hit;

		// if the ray hit something ,gaze point will show at the hitpoint,and scale\rotate itself by distance;
		if (Physics.Raycast (ray, out hit, 100))
        {
			canvasPoint.transform.localScale = hit.distance * canvasOriginalScale * 0.005f;
			canvasPoint.transform.position = hit.point;
			//hit normal will return the v3 direction of hit surface point;
			canvasPoint.transform.forward = hit.normal;


			//when we hit the new obj...,or this is first time we got a obj;
			if (hit.transform.gameObject != curInteractiveObj)
            {
				//we should store the obj as old one(before it been renew),as we want to do something on old obj;
				if (curInteractiveObj != null)
                {
					oldInteractiveObj = curInteractiveObj;
					oldobjDoing=oldInteractiveObj.GetComponent<Peter_InteractiveObj> ();
					if (oldobjDoing)
                    {
						oldobjDoing.leave ();
					}
				}
				curInteractiveObj = hit.transform.gameObject;
				//try to get the Peter_... on this obj
				objDoing = curInteractiveObj.GetComponent<Peter_InteractiveObj> ();
				if(objDoing){
					objDoing.hovered ();
				}
			} 

			//when the obj is not changed(still being gazed)...start count time 
			else
            {
				if (IVRInput.GetKeyDown())
                {
                    objDoing = curInteractiveObj.GetComponent<Peter_InteractiveObj>();
                    if (objDoing)
                    {
                        objDoing.activated();
                    }
				}
			}

		} 
            else {
			// user's point leaved,so our point need to reset;
			canvasPoint.transform.localPosition = canvasOriginalPosition;
			canvasPoint.transform.localEulerAngles = Vector3.zero;
			canvasPoint.transform.localScale = canvasOriginalScale;
			//gaze leaved, leave function should be exetuted,and set curobj as null;
			if (curInteractiveObj != null) {
			    objDoing=curInteractiveObj.GetComponent<Peter_InteractiveObj> ();
				if (objDoing) {
					objDoing.leave ();
					curInteractiveObj = null;
				}
			}
		}
	}
}
